package com.dizang.ddd.entity;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.dizang.ddd.constants.GlobalConstant;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper=false)
public class PomBuilder extends AbstractPomBuilder {

    protected String artifactId;

    protected String parentArtifactId;

    protected String groupId;
    
    protected List<AbstractProjectBuilder> dependencies = new ArrayList<>();
    
    protected List<AbstractProjectBuilder> modules;

    public PomBuilder() {
        
    }
    
    public PomBuilder(String artifactId, String parentArtifactId, String groupId,
            List<AbstractProjectBuilder> dependencies, List<AbstractProjectBuilder> modules) {
        super();
        this.artifactId = artifactId;
        this.parentArtifactId = parentArtifactId;
        this.groupId = groupId;
        this.dependencies = dependencies;
        this.modules = modules;
    }

    @Override
    public Element buildBaseNode(Document document, Element project) {

        if (getParentArtifactId() != null) {

            Element parent = document.createElement("parent");
            project.appendChild(parent);

            Element groupId = document.createElement("groupId");
            groupId.setTextContent(getGroupId());
            parent.appendChild(groupId);

            Element parentArtifactId = document.createElement("artifactId");
            parentArtifactId.setTextContent(getParentArtifactId());
            parent.appendChild(parentArtifactId);

            Element version = document.createElement("version");
            version.setTextContent(GlobalConstant.version);
            parent.appendChild(version);
        }
        Element modelVersion = document.createElement("modelVersion");
        modelVersion.setTextContent(GlobalConstant.modelVersion);
        project.appendChild(modelVersion);
        Element artifactId = document.createElement("artifactId");
        artifactId.setTextContent(getArtifactId());
        project.appendChild(artifactId);

        Element packaging = document.createElement("packaging");
        packaging.setTextContent("jar");
        project.appendChild(packaging);
        return project;
    }

    @Override
    public Element buildPropertiesNode(Document document, Element project) {
        return project;
    }

    @Override
    public Element buildModuleNode(Document document, Element project) {
        return project;
    }

    @Override
    public Element buildDependenciesNode(Document document, Element project) {
        List<AbstractProjectBuilder> dependencies2 = getDependencies();
        Element dependencies = document.createElement("dependencies");
        project.appendChild(dependencies);
        for (AbstractProjectBuilder each : dependencies2) {

            Element dependency = document.createElement("dependency");
            dependencies.appendChild(dependency);

            Element groupId = document.createElement("groupId");
            groupId.setTextContent(each.getGroupId());
            dependency.appendChild(groupId);

            Element artifactId = document.createElement("artifactId");
            artifactId.setTextContent(each.getArtifactId());
            dependency.appendChild(artifactId);

            Element version = document.createElement("version");
            version.setTextContent("${project.version}");
            dependency.appendChild(version);
        }
        return project;
    }


}
